<style>

</style>
<div class="layui-layout book-rating-wrapper" style="padding:0 20px;">
    <div id="search-wrapper">
        <div class="layui-form">
            <div class="layui-row">
                <div class="layui-col-md4">
                    <div class="layui-inline inline-input">
                        <select name="confirm" class="confirm" lay-verify="">
                            <option value="-1">全部</option>
                            <option value="0">未审核</option>
                            <option value="1">已审核</option>
                        </select>
                    </div>
                    <div class="layui-inline">
                        <div class="layui-inline">
                            <input class="layui-input name" name="name"  placeholder="请输入书名">
                        </div>
                        <div class="layui-inline">
                            <button class="layui-btn search" data-type="reload">搜索</button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <table class="layui-hide" id="comment_list" lay-filter="chapter_filter">
    </table>
</div>
<script>
    $(function () {
        console.log(getQueryString()["name"]);
        $(".name").val(getQueryString()["name"]);
    });
    layui.use(['table','form'], function(){
        var table = layui.table;
        var $ = layui.$;
                //方法级渲染
        table.render({
            elem: '#comment_list'
            ,url: '/bookrating/table'
            ,where:{confirm:$('.confirm').val(),name:$(".name").val()}
            ,cols: [[
                {field:'Sort', title: '序号', align:'center',width:100},
                {field:'UserId', title: '用户ID', align:'center',width:120},
                {field:'Name', title: '书名', align:'center',width:200},
                {field:'Message', title: '评论内容', align:'center'},
                {field:'StarScore', title: '星级评分', align:'center',width:100},
                {field:'CreateDate', title: '提交时间', align:'center',width:180,templet: function(d){
                        return (d.CreateDate+"").split("+")[0].replace("T"," ");
                    }},
                {field:'UpdateDate', title: '审核时间', align:'center',width:180,templet: function(d){
                        if(d.UpdateDate+"" == "0001-01-01T00:00:00Z"){
                            return ""
                        }else{
                            return (d.UpdateDate+"").split("+")[0].replace("T"," ");
                        }
                    }},
                {field:'IsConfirm', title: '状态', align:'center',width:120,templet: function(d){
                        if(d.IsConfirm == 0){
                            return "未审核";
                        }else if(d.IsConfirm == 1){
                            return "已上线";
                        }
                    }},
                {field:'', title: '操作', align:'center',width:180,templet: function(d){
                        if(d.IsConfirm == 0){
                            return `<button class="layui-btn layui-btn-xs layui-btn-normal" onclick="checkComment('`+ d.Id +`')">审核</button>
                                    <button class="layui-btn layui-btn-xs layui-btn-danger" onclick="delComment('`+ d.Id +`')">删除</button>`
                        }else if(d.IsConfirm == 1){
                            return `<button class="layui-btn layui-btn-xs layui-btn-normal layui-btn-disabled">已审核</button>
                                    <button class="layui-btn layui-btn-xs layui-btn-danger" onclick="delComment('`+ d.Id +`')">删除</button>`;
                        }
                    }}
            ]]
            ,id: 'listReload'
            ,page: {
                curr: location.hash.replace('#!fenye=', '') //获取起始页
                ,hash: 'fenye' //自定义hash值
            }
            ,height: "full-200"
        });

        // 选择书名或者作者名
        var active = {
            reload: function(){
                table.reload('listReload', {
                    where: {
                        name:$(".name").val(),
                        confirm:$('.confirm').val()
                    }
                    ,page: {
                        curr: 1 //重新从第 1 页开始
                    }
                });
            }
        };

        $('#search-wrapper .search').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });

    function checkComment(id) {
        $.ajax({
            url: '{{urlfor "BookratingController.Check"}}',
            data:{'id':id},
            type: 'PUT',
            success: function( response ) {
                if(response.status == 0){
                    $(".layui-laypage-btn").click();
                    layer.msg(response.message);
                }else {
                    layer.msg(response.message);
                }
            }
        });
    }

    function delComment(id) {
        layer.open({
            title:'提示',
            content: '确定要要删除吗？',
            yes: function(index, layero){
                //do something
                $.ajax({
                    url: '{{urlfor "BookratingController.Del"}}',
                    data:{'id':id},
                    type: 'put',
                    success: function( response ) {
                        if(response.status == 0){
                            $(".layui-laypage-btn").click();
                            layer.msg(response.message);
                        }else {
                            layer.msg(response.message);
                        }
                    }
                });
                layer.close(index); //如果设定了yes回调，需进行手工关闭
            }
        });
    }

    function getQueryString() {
        var qs = location.search.substr(1), // 获取url中"?"符后的字串
                args = {}, // 保存参数数据的对象
                items = qs.length ? qs.split("&") : [], // 取得每一个参数项,
                item = null,
                len = items.length;

        for(var i = 0; i < len; i++) {
            item = items[i].split("=");
            var name = decodeURIComponent(item[0]),
                    value = decodeURIComponent(item[1]);
            if(name) {
                args[name] = value;
            }
        }
        return args;
    }
</script>